package com.personal.kousuan;

import java.util.ArrayList;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.personal.kousuan.db.CommonData;
import com.personal.kousuan.db.CommonData.Category;
import com.personal.kousuan.db.CommonData.Semester;

public class SettingActivity extends Activity implements AdapterView.OnItemSelectedListener {

	private Spinner numberRangeSpn;
	private Spinner gradeSpn;
	private Spinner categorySpn;
	
	private ArrayList<Integer> semesterIds = new ArrayList<Integer>();
	private ArrayList<String> semesters = new ArrayList<String>();;
	private ArrayList<String> numbers = new ArrayList<String>();
	private ArrayList<Integer> categoryIds = new ArrayList<Integer>();;
	private ArrayList<String> categoryKeys = new ArrayList<String>();
	private ArrayList<String> categoryNames = new ArrayList<String>();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		numberRangeSpn = (Spinner)findViewById(R.id.number_range_spn);
		gradeSpn = (Spinner)findViewById(R.id.grade_spn);
		categorySpn = (Spinner)findViewById(R.id.category_spn);
		
		//load Semester
		CommonData commondata = CommonData.getInstance();
		for(Semester semester: commondata.semesters.values()){
			semesterIds.add(semester.getId());
			semesters.add(semester.getName());
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item_gravity_right, semesters);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		gradeSpn.setAdapter(adapter);
		gradeSpn.setOnItemSelectedListener(this);
		
		//initial number range
		numbers.add("20");
		numbers.add("40");
		numbers.add("60");
		numbers.add("80");
		numbers.add("100");
		ArrayAdapter<String> numberRangeAdapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item_gravity_right, numbers);
		numberRangeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		numberRangeSpn.setAdapter(numberRangeAdapter);
		
		categorySpn.setOnItemSelectedListener(this);
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		if(parent == gradeSpn){
			int semesterId = semesterIds.get(position);
			CommonData commondata = CommonData.getInstance();
			Map<String,Category> categoryMap = commondata.categorys.get(semesterId);
			categoryIds.clear();
			categoryKeys.clear();
			categoryNames.clear();
			for(Category category:categoryMap.values()){
				categoryIds.add(category.getId());
				categoryKeys.add(category.getCategoryKey());
				categoryNames.add(category.getCategoryName());
			}
			
			// refresh listview
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_spinner_item_gravity_right, categoryNames);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			categorySpn.setAdapter(adapter);
		}else if(parent == categorySpn){
			
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		if(parent == gradeSpn){
			categorySpn.setAdapter(null);
		}
	}
}
